package com.woniuxy01;

import java.util.*;

/**
 * @author 作者
 * @create 2025-11-06 14:39
 */
public class Test05 {
    public static void main(String[] args) {
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 15);
        Student s3 = new Student(3, "ww", 12);

        ArrayList<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
//        Collections.sort(students, (o1, o2) -> o2.getAge() - o1.getAge());

        students.stream()
                .sorted((o1, o2) -> o2.getAge() - o1.getAge())
                .forEach(System.out::println);

        students.forEach(System.out::println);
    }

    private static void test04() {
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 15);
        Student s3 = new Student(3, "ww", 12);

        ArrayList<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        //匿名内部类：接口的实现类对象
        Comparator<Student> comparator = new Comparator<>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        };
        Collections.sort(students, comparator);

        System.out.println(students);
    }

    private static void test03() {
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 15);
        Student s3 = new Student(3, "ww", 12);

        ArrayList<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        System.out.println("排序前");
        System.out.println(students);
        Collections.sort(students);
        System.out.println("自然排序：年龄升序");
        System.out.println(students);
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o2.getAge() - o1.getAge();
            }
        });
        System.out.println("自定义排序：年龄降序");
        System.out.println(students);
        Collections.sort(students, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                return o1.getName().compareTo(o2.getName());
            }
        });
        System.out.println("自定义排序：姓名升序");
        System.out.println(students);
    }

    private static void test02() {
        Student s1 = new Student(1, "zs", 11);
        Student s2 = new Student(2, "ls", 15);
        Student s3 = new Student(3, "ww", 12);

        ArrayList<Student> students = new ArrayList<>();
        students.add(s1);
        students.add(s2);
        students.add(s3);
        System.out.println("排序前");
        System.out.println(students);
        //对集合中的学生排序，自定义冒泡算法实现
        for (int i = 0; i < students.size() - 1; i++) {
            for (int j = 0; j < students.size() - 1 - i; j++) {
                if (students.get(j).getAge() > students.get(j + 1).getAge()) {
                    Student temp = students.get(j);
//                    students.get(j)=students.get(j+1);
//                    students.get(j+1)=temp;
                    students.set(j, students.get(j + 1));
                    students.set(j + 1, temp);
                }
            }
        }
        System.out.println("排序后");
        System.out.println(students);
    }

    private static void test01() {
        //        for (int i=1;i<=5;i++) {
//            System.out.println(Math.random());
//        }

        Random random = new Random();
        for (int i = 1; i <= 10; i++) {
            System.out.println(random.nextInt(1, 20));
        }
    }


}
